package com.ccsu.mymarket.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccsu.mymarket.controller.utils.DataUtils;
import com.ccsu.mymarket.dao.CatagoryDao;
import com.ccsu.mymarket.entity.Book;
import com.ccsu.mymarket.entity.Catagory;
import com.ccsu.mymarket.exception.MyException;
import com.ccsu.mymarket.service.BookService;
import com.ccsu.mymarket.service.CatagoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CatagoryServiceImpl extends ServiceImpl<CatagoryDao, Catagory> implements CatagoryService {
    private final CatagoryDao catagoryDao;
    private final BookService bookService;

    @Autowired
    public CatagoryServiceImpl(CatagoryDao catagoryDao, @Autowired BookService bookService) {
        this.catagoryDao = catagoryDao;
        this.bookService = bookService;
    }

    @Override
    public List<Catagory> getCatagory(Integer type) {
        List<Catagory> catagoryList = catagoryDao.selectList(new LambdaQueryWrapper<Catagory>()
                .eq(type != null, Catagory::getCataType, type)
                .select(Catagory::getId,Catagory::getCatagName));
        return catagoryList;
    }

    @Override
    public boolean deleteCatagory(Integer id, Integer type) {
//         非最底层分类，需级联删除子级分类
        switch (type) {
            case 1:
                return deleteLevelOneCatagory(id);
            case 2: {
//                    删除二级分类
                if (this.removeById(id)
                        &&
//                            删除三级分类
                        this.remove(new LambdaQueryWrapper<Catagory>().eq(Catagory::getParentCatag, id))) {
                    return true;
                }
            }
            default:
                return this.removeById(id);
        }
    }

    @Override
    public boolean ClassChildBookExist(Integer id, Integer type) {
//         根据分类级别查找相关图书数量
        long bookCount = 0;
//        对三级分类
        if (type == 3) {
            bookCount = bookService.count(new LambdaQueryWrapper<Book>().eq(Book::getClassification, id));
            if (bookCount > 0) {
                return true;
            } else
                return false;
        }

        List<Integer> fatherIds = new ArrayList<>();
        List<Integer> typeIdList = null;

//            先查看一下子分类的id
        fatherIds.add(id);
        fatherIds = getChildrenIds(fatherIds);
        if (fatherIds == null || fatherIds.size() < 1)
            return false;

//         二级分类 或 三级分类 为空时，需终止查询
        switch (type) {
//              一级分类要查两遍子分类id，所以还要查一遍
            case 1:
                typeIdList = getChildrenIds(fatherIds);

                if (typeIdList == null || typeIdList.size() < 1)
                    return false;

                bookCount = bookService.count(new LambdaQueryWrapper<Book>()
                        .in(Book::getClassification, typeIdList));

                if (bookCount > 0) {
                    return true;
                } else {
                    return false;
                }
//              二级分类只用查一遍子分类
            case 2:
                typeIdList = fatherIds;
                bookCount = bookService.count(new LambdaQueryWrapper<Book>()
                        .in(Book::getClassification, typeIdList));
                if (bookCount > 0) {
                    return true;
                }
        }
        return false;
    }

    @Override
    public List<Book> searchBookByCatagory(Integer id, Integer type) throws MyException {
        List<Book> bookList=null;
        switch (type){
            case 1:{
                List<Integer> sonIds=getChildrenIds(id);
                if (sonIds==null || sonIds.size()<0)
                    throw new MyException("该一级分类下无二级分类!");
                List<Integer> grandSonIds=getChildrenIds(sonIds);
                if (grandSonIds==null || grandSonIds.size()<0)
                    throw new MyException("该一级分类下无三级分类!");
                bookList=bookService.list(new LambdaQueryWrapper<Book>()
                        .in(Book::getClassification,grandSonIds));
                break;
            }
            case 2:
            {
                List<Integer> sonIds=getChildrenIds(id);
                if (sonIds==null || sonIds.size()<0)
                    throw new MyException("该二级分类下无三级分类!");
                bookList=bookService.list(new LambdaQueryWrapper<Book>()
                        .in(Book::getClassification,sonIds));
                break;
            }
            case 3:{
                bookList=bookService.list(new LambdaQueryWrapper<Book>()
                        .eq(Book::getClassification,id));
                break;
            }
        }
        return bookList;
    }

    private boolean deleteLevelOneCatagory(Integer id) {
        //删除该一级分类
        this.removeById(id);
//        取该分类下的二级分类
        List<Catagory> catagoryList = this.list(new LambdaQueryWrapper<Catagory>()
                .eq(Catagory::getParentCatag, id).select(Catagory::getId));
//        无二级子分类，终止删除
        if (catagoryList == null || catagoryList.size() < 1)
            return true;
//        有二级子分类
        List<Integer> fatherIds = catagoryList.stream()
                .map(Catagory::getId).collect(Collectors.toList());
//        删除可能存在的三级子分类
        if (this.remove(new LambdaQueryWrapper<Catagory>()
                .in(Catagory::getParentCatag, fatherIds)))
            return true;
        return false;
    }
//查询子级分类id,传入父级分类id 的 list
    private List<Integer> getChildrenIds(List<Integer> fatherIds) {
        return catagoryDao.selectList(new LambdaQueryWrapper<Catagory>()
                        .in(Catagory::getParentCatag, fatherIds).select(Catagory::getId))
                .stream().map(Catagory::getId).collect(Collectors.toList());
    }
    //查询子级分类id,传入父级分类id
    private List<Integer> getChildrenIds(Integer fatherId) {
        return catagoryDao.selectList(new LambdaQueryWrapper<Catagory>()
                        .eq(Catagory::getParentCatag, fatherId).select(Catagory::getId))
                .stream().map(Catagory::getId).collect(Collectors.toList());
    }

}
